﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using OpenFlashChart;
using Peak.ApplicationServices.Services;
using Peak.Core;
using Peak.Core.DataInterfaces;
using Peak.Resources;
using Peak.Web.Controllers.Services;
using SharpArch.Core.PersistenceSupport;
using Peak.Web.Controllers.Models;
using Microsoft.Web.Mvc;
using System.Threading;
using SharpArch.Web.NHibernate;

namespace Peak.Web.Controllers
{
  public class DashboardController : BaseController
  {
    private readonly ITaskService _taskService;

    public DashboardController(ISessionService sessionService, ITaskService taskService)
      : base(sessionService)
    {
      _taskService = taskService;
    }

    public ActionResult Index(DashboardFilterModel filter)
    {
      Page<Task> tasks = _taskService.GetPage(CreateTaskFilter(filter));

      _sessionService.BackLink.Reset();

      int? sprintId = null;
      if(_sessionService.Project.CurrentSprint != null)
      {
        sprintId = _sessionService.Project.CurrentSprint.Id;
      }

      Dictionary<User, UserActivityModel> users = new Dictionary<User, UserActivityModel>();
      foreach (User user in _sessionService.Project.Users)
      {
        users.Add(user, new UserActivityModel(user));
      }
      Page<Task> inProgressTasks = _taskService.GetPage(new TaskFilter()
      {
        Status = FilterTaskStatus.InProgress | FilterTaskStatus.ReadyForTest,
        Assigned = true,
        SprintId = sprintId,
        ProjectId = _sessionService.Project.Id,
      });
      
      foreach (Task task in inProgressTasks.Results)
      {
        users[task.AssignedTo].CurrentTasks.Add(task);
      }

      Page<Task> nextTasks = _taskService.GetPage(new TaskFilter()
      {
        ProjectId = _sessionService.Project.Id,
        SprintId = sprintId,
        Status = FilterTaskStatus.ToDo,
        Assigned = true
      });
      
      foreach (Task task in nextTasks.Results)
      {
        users[task.AssignedTo].NextTasks.Add(task);
      }

      DashboardModel dashboard = new DashboardModel(filter, _sessionService.Project.Users, users.Values.OrderBy(f => f.User.UserName).ToList());
      dashboard.AddTasks(tasks.Results);
      dashboard.Sort();

      return View(dashboard);
    }

    private TaskFilter CreateTaskFilter(DashboardFilterModel filter)
    {
      TaskFilter result = new TaskFilter();
      if (filter.StatusToDo)
        result.Status |= FilterTaskStatus.ToDo;
      if (filter.StatusInProgress)
        result.Status |= FilterTaskStatus.InProgress;
      if (filter.StatusDone)
        result.Status |= FilterTaskStatus.Done;

      result.AssignedTo = filter.AssignedTo;
      result.Assigned = filter.Assigned;
      result.QuickFilter = filter.QuickFilter;
      result.ProjectId = _sessionService.Project.Id;
      if (filter.CurrentSprintOnly && _sessionService.Project.CurrentSprint != null)
      {
        result.SprintId = _sessionService.Project.CurrentSprint.Id;
      }
      return result;
    }
  }
}
